
#include "Main.h"

#include "Input.h"

void Input::Platform_SetCursor( const INPUT_MOUSE_CURSOR xCursor )
{
	static bool s_bInitialised = false;
	static HCURSOR s_xCursors[INPUT_NUM_MOUSE_CURSORS];
	
	if( !s_bInitialised )
	{
		s_xCursors[INPUT_MOUSE_CURSOR_ARROW] = LoadCursor(NULL, IDC_ARROW);
		s_xCursors[INPUT_MOUSE_CURSOR_CARET] = LoadCursor(NULL, IDC_IBEAM);

		s_bInitialised = true;
	}	

	::SetCursor( s_xCursors[ xCursor ] );
}

// ____________________________________________________________________

void Clipboard::Platform_Copy()
{
	const int iLen = strlen( s_szClipboard );

	if( iLen == 0 )
	{
		return;
	}

	HGLOBAL xGlobHandle; 

	// Open the clipboard, and empty it. 
 
	if ( !OpenClipboard( NULL ) )
	{
		FAssert( false );
		return; 
	}
	EmptyClipboard();

	// Allocate a global memory object for the text. 
 
	xGlobHandle = GlobalAlloc( GMEM_MOVEABLE, iLen + 1 ); 
	if( xGlobHandle == NULL )
	{ 
		CloseClipboard(); 
		return;
	} 
 
	// Lock the handle and copy the text to the buffer. 
 
	LPTSTR szCopy = (LPTSTR)GlobalLock( xGlobHandle ); 
	memcpy( szCopy, s_szClipboard, iLen ); 
	szCopy[iLen] = 0;
	GlobalUnlock( szCopy ); 

	// Place the handle on the clipboard. 
 
	SetClipboardData( CF_TEXT, szCopy ); 
	
	CloseClipboard(); 
}

void Clipboard::Platform_Paste()
{
	if ( !IsClipboardFormatAvailable( CF_TEXT ) ) 
	{
		return;
	}

	if( !OpenClipboard( NULL ) ) 
	{
		return;
	}
	
	HGLOBAL xGlobHandle = ::GetClipboardData( CF_TEXT );
	if( xGlobHandle != NULL )
	{
		const char* szText = (const char*)GlobalLock( xGlobHandle );
		const int iLen = MACRO_MIN( strlen( szText ), iCLIPBOARD_DATA_SIZE - 1 );
		
		strncpy( s_szClipboard, szText, iLen );
		s_szClipboard[ iLen ] = 0;

		GlobalUnlock( xGlobHandle ); 
	} 
	CloseClipboard();
}

// eof
